package org.pi.common.plot.drawables

import org.pi.common.geom.Point

class Function( val points: List[Point]) extends Drawable with org.pi.common.observerpattern.ObservableImpl { // TODO with Observable
  private var stroke : java.awt.Stroke = null

  private var _lineWidth : Float = 0
  def lineWidth = _lineWidth
  def lineWidth_=(w: Float) = {
    stroke = new java.awt.BasicStroke(w)
    _lineWidth = w
    notifyObservers()
  }
  this.lineWidth = Drawable.lineWidth

  private var _color = Drawable.paintColor
  def color = _color
  def color_=(c: java.awt.Color) = {
	  _color = c
	  notifyObservers()
  }

  override def draw( f: (Float, Float) => (Float, Float), graphics : java.awt.Graphics2D ) {
    graphics.setColor(color)
    graphics.setStroke(stroke)
    for (i: Int <- 0 until points.length-1)
    {
    	val j : Int = (i+1)%points.length
    	val (x1,y1) = f(points(i).x, points(i).y)
    	val (x2,y2) = f(points(j).x, points(j).y)
    	graphics.draw( new java.awt.geom.Line2D.Float( x1,y1,x2,y2 ))
    }
  }
  override def getMinXyMaxXy() : (Float,Float,Float,Float) = {
    (
      points.foldLeft( points(0).x){(x,p)=>Math.min(x,p.x)},
      points.foldLeft( points(0).y){(y,p)=>Math.min(y,p.y)},
      points.foldLeft( points(0).x){(x,p)=>Math.max(x,p.x)},
      points.foldLeft( points(0).y){(y,p)=>Math.max(y,p.y)}
    )
  }
}
